#region Imports
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using InsightHumans = PHSRAG.Insight21.Humans;  
#endregion

namespace PHSRAG.Insight21.InsightWebControls
{
	/// <summary>
	///  Navigation Bar for CoI Overview Pages
	/// </summary>
	[ToolboxData("<{0}:CoIAdminSubNavigationrunat=server />")]
	[Designer(typeof(CoIAdminSubNavigationDesigner), typeof(IDesigner))]
	public class COIAdminSubNavigation: SubNavigation
	{
		#region Constants
		private const string selectedCoIAdminSubNavigationLinkKey = 
			"SelectedCoIAdminSubNavigationLink_B2011BFB-0D55-4845-8E7C-2BD2E80876C0";
        #endregion

		#region Instance Variables
		private LinkButton dataManagementLinkButton; 
		private LinkButton communicationManagementLinkButton; //Notification management		
        private LinkButton delegatesManagementLinkButton;
		#endregion

        #region Implementations for abstract properties and methods from Base Class SubNavigation

            /// <summary>
		/// Create all controls in the CoISubNavigation.
		/// </summary>
		protected override void CreateControls()
		{
            Controls.Clear();

			dataManagementLinkButton = new LinkButton();
			dataManagementLinkButton.CommandName = dataManagementLinkButton.Text = "Data Management";
			dataManagementLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.CoI.DataManagementURL", "");
            
            communicationManagementLinkButton = new LinkButton();
			communicationManagementLinkButton.CommandName = communicationManagementLinkButton.Text = "Communication Management";
			communicationManagementLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.CoI.CommunicationManagementURL", "");

            delegatesManagementLinkButton = new LinkButton();
            delegatesManagementLinkButton.CommandName = delegatesManagementLinkButton.Text = "Delegate Management";
            delegatesManagementLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.CoI.DelegatesManagementURL", "");
         				
		}
		/// <summary>
		/// Setup the control tree to reflect the actual (and exact) layout of the controls 
		/// within the CoIAdminSubModNavigation.
		/// </summary>
		protected override void GenerateControlTree()
		{
			Table table = new Table();
			Controls.Add(table);
			table.CssClass = "SubModuleSummaryNavigation";
			table.EnableViewState = false;

			TableRow row = new TableRow();
			table.Rows.Add(row);

			TableCell cell = new TableCell();
			row.Cells.Add(cell);
			cell.CssClass = "SubModuleSummaryNavigationFillerCell";
			cell.Controls.Add(new LiteralControl("&nbsp;"));

			cell = new TableCell();
			row.Cells.Add(cell);

			table = new Table();
			cell.Controls.Add(table);

			row = new TableRow();
			table.Rows.Add(row);

			cell = new TableCell();
			row.Cells.Add(cell);

			cell = new TableCell();
			row.Cells.Add(cell);
			cell.Controls.Add(dataManagementLinkButton);

			cell = new TableCell();
			row.Cells.Add(cell);
            cell.Controls.Add(communicationManagementLinkButton);

            cell = new TableCell();
			row.Cells.Add(cell);
			cell.Controls.Add(delegatesManagementLinkButton);

			CreateSelectedLinkAdornments(row);
		}

		/// <summary>
		/// The selected link cell is given a prefix and suffix image as adornments. In addition the background of the
		/// cell is of a different style.
		/// </summary>
		private void CreateSelectedLinkAdornments(TableRow row)
		{
			LinkButton selectedLink = GetSelectedLink();
			if (selectedLink != null)
			{
				foreach (TableCell cell in row.Cells)
				{
					if ((cell.Controls.Count == 1) && (cell.Controls[0] == selectedLink))
					{
						cell.CssClass = "SubModuleNavigationSelectedSummary";

						TableCell adornmentCell = new TableCell();
						adornmentCell.CssClass = "SubModuleNavigationSelectedSummaryAdornment";
						row.Cells.AddAt(row.Cells.GetCellIndex(cell), adornmentCell);

						const string prefixImage = "../Images/Row2_Left.gif";
						System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(Context.Server.MapPath(prefixImage));
						Image image = new Image();
						image.ImageUrl = prefixImage;
						adornmentCell.Width = bitmap.Width;
						adornmentCell.Controls.Add(image);

						adornmentCell = new TableCell();
						adornmentCell.CssClass = "SubModuleNavigationSelectedSummaryAdornment";
						row.Cells.AddAt(row.Cells.GetCellIndex(cell) + 1, adornmentCell);

						const string suffixImage = "../Images/Row2_Right.gif";
						bitmap = new System.Drawing.Bitmap(Context.Server.MapPath(suffixImage));
						image = new System.Web.UI.WebControls.Image();
						image.ImageUrl = suffixImage;
						adornmentCell.Width = bitmap.Width;
						adornmentCell.Controls.Add(image);

						break;
					}
				}
			}
		}

      
		/// <summary>
		/// Retrieve the name of the link that was last clicked on.
		/// </summary>
		/// <returns>Reference to the link button corresponding to the clicked link</returns>
		protected override LinkButton GetSelectedLink()
		{
			// Anytime a link is clicked on, the OnBubbleEvent handler stores the name of the link 
			// in the session. Retrieve that name and return the corresponding link button reference.

			object o = Page.Session[selectedCoIAdminSubNavigationLinkKey];
			if (o != null)
			{
				string commandName = (string)o;
				if (commandName == dataManagementLinkButton.CommandName)
				{
					return dataManagementLinkButton;
				}
				if (commandName == communicationManagementLinkButton.CommandName)
				{
					return communicationManagementLinkButton;
				}

                if (commandName == delegatesManagementLinkButton.CommandName)
                {
                    return delegatesManagementLinkButton;
                }
			}			
			return dataManagementLinkButton;
		}

		/// <summary>
		/// 
		/// </summary>
		protected override string SelectedSubNavigationLinkKey
		{
			get 
			{
				return selectedCoIAdminSubNavigationLinkKey;
			}
		}
		/// <summary>
		/// Set the selected link so that on load, the appropriate tab may be shown highlighted. This property is invoked
		/// by pages while vectoring to other pages which host this navigation control.
		/// </summary>
		public string SelectedLink
		{
			set { Page.Session[SelectedSubNavigationLinkKey] = value; }
		}
		#endregion
	}
	#region Class CoIAdminSubNavigationDesigner
	/// <summary>
	/// Designer associated with the control.
	/// </summary>
	public class CoIAdminSubNavigationDesigner : ControlDesigner
	{
		#region Overrides
		/// <summary>
		/// Force the control to create its child controls for display at design time.
		/// </summary>
		/// <returns>HTML representation of the control</returns>
		public override string GetDesignTimeHtml()
		{
			((COIAdminSubNavigation)Component).Design();
			return base.GetDesignTimeHtml();
		}
		#endregion
	}
		#endregion
}
